//
//  EditInfoTopView.m
//  QT
//
//  Created by 甘世清 on 2025/1/7.
//

#import "EditInfoTopView.h"

@implementation EditInfoTopView

- (void)SQ_UIViewForSelfView{
    
    [self addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.size.mas_equalTo(120);
        make.bottom.equalTo(self.mas_bottom).offset(-40);
    }];
    UIImageView * imageView = [[UIImageView alloc]init];
    imageView.image = SQGetImage(@"xiangji");
    imageView.backgroundColor = SQColorF1F1F1;
    imageView.layer.cornerRadius = 20;
    imageView.layer.masksToBounds = YES;
    [self.headerView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(40);
            make.bottom.right.equalTo(self.headerView);
    }];
    [self.headerView.imageView sd_setImageWithURL:SQURLImage([UserModel share].portrait) placeholderImage:SQGetImage(@"1")];
    WS(weakSelf);
    [self.headerView setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_HeaderViewqButtonClick];
    }];
}

-(void)SQ_HeaderViewqButtonClick{
    WS(weakSelf);
    ImagePopView * vc = [[ImagePopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 200+FootHeight)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
   
    [vc setAddImageArrayBlock:^(NSArray<UIImage *> * _Nonnull imageArray) {
        [weakSelf SQ_uplodImage:imageArray.firstObject];
        [wk_pop dismiss];
    }];
}
-(void)SQ_uplodImage:(UIImage*)image{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    WS(weakSelf);
    [HttpTool SQ_Image:image successBlock:^(BaseModel * _Nonnull httpModel) {
        NSString * imageurl = [NSString stringWithFormat:@"%@",httpModel.data];
        [weakSelf SQ_updateImage:imageurl];
        
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"网络异常请重试")];
    }];
}
-(void)SQ_updateImage:(NSString*)headerImage{
    WS(weakSelf);
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:headerImage forKey:@"portrait"];
    [HttpTool post:@"user/config" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel  * model = [UserModel share];
        model.portrait = headerImage;
        [UserModel updateLocalUserModel:model];
        [weakSelf.headerView.imageView sd_setImageWithURL:SQURLImage([UserModel share].portrait) placeholderImage:SQGetImage(@"1")];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
       [YJProgressHUD showMsgWithoutView:httpModel.message];
       
    }];
}
- (SQCustomButton *)headerView{
    if (!_headerView) {
        _headerView = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(120, 120) midmargin:0];
        _headerView.imageView.clipsToBounds = YES;
        _headerView.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _headerView.imageView.layer.cornerRadius = 60;
        _headerView.imageView.layer.masksToBounds = YES;
        _headerView.imageView.layer.borderWidth = 1;
        _headerView.imageView.layer.borderColor = UIColor.whiteColor.CGColor;
    }
    return _headerView;
}

@end
